package com.example.myapplication;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity3 extends AppCompatActivity implements Runnable{
    EditText s2;
    TextView s1;
    float dollarrate=0.14f;
    float eurorate=0.13f;
    float wonrate=200.73f;
    Handler handler;
    private String inputStream2String(InputStream inputStream) throws IOException{
        final int bufferSize=1024;
        final char[] buffer=new char[bufferSize];
        final StringBuilder out=new StringBuilder();
        Reader in=new InputStreamReader(inputStream,"gb2312");
        while(true){
            int rsz=in.read(buffer,0,buffer.length);
            if(rsz<0)
                break;
            out.append(buffer,0,rsz);

        }
        return out.toString();
    }
        public void run(){
            Log.i(TAG,"run:run()...");
            Message msg=handler.obtainMessage(5);
            msg.obj="Hello";
            handler.sendMessage(msg);
            URL url=null;
            try{
                url=new URL("www.usd-cny.com/bankofchina.htm");
                HttpURLConnection http=(HttpURLConnection)url.openConnection();
                InputStream in=http.getInputStream();
                String html=inputStream2String(in);
                Log.i(TAG,"run:html="+html);
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main3);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            s1=findViewById(R.id.os);
            s2=findViewById(R.id.is);
            Bundle bundle=getIntent().getExtras();
            if(bundle!=null){
                dollarrate=bundle.getFloat("dr");
                eurorate=bundle.getFloat("er");
                wonrate=bundle.getFloat("wr");
            }
            handler=new Handler(){
                public void handleMessage(@NonNull Message msg){
                    if(msg.what==5){
                        String str=(String)msg.obj;
                        Log.i(TAG,"handleMessage");

                    }
                    super.handleMessage(msg);
                }
            };
            Thread t=new Thread(this);
            t.start();


            return insets;
        });
    }

    public void click(View btn){
        Log.i(TAG,"click:");
        if(btn.getId()==R.id.btn4){
            Intent main=new Intent(MainActivity3.this, dollarRateConfig.class);
            startActivity(main);
        }
        String is=s2.getText().toString();
        double rmb=Double.parseDouble(is);
        double res=0;
        if(btn.getId()==R.id.btn1){
            res=dollarrate*rmb;
        }
        else if(btn.getId()==R.id.btn2){
            res=eurorate*rmb;
        }

        else{
            res=wonrate*rmb;
        }
        s1.setText(String.valueOf(res));
    }

}